﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class WordsSpeak : MonoBehaviour {

    public GameObject ALine; //一列的预制体
    public GameObject Word;  //每个字的预制体
    private List<GameObject> WordsList = new List<GameObject>();
    private int index = 0;
    private float timer = 0;
    private bool timerStart = false;

    public static string Words = "天高云淡，望断南飞雁。不到长城非好汉，屈指行程二万。六盘山上高峰，红旗漫卷西风。今日长缨在手，何时缚住苍龙。";


    private void Start()
    {
        ShowWords();
    }

    private void FixedUpdate()
    {
        if (index >= WordsList.Count)
        {
            return;
        }
        TimeDelay(3,0.27f);
    }

    //跳字
    private void ShowWords()
    {
        GameObject WordLine = Instantiate(ALine);
        WordLine.transform.SetParent(this.gameObject.transform);
        WordLine.transform.localScale = Vector3.one;
        char[] chars = Words.ToCharArray();
        foreach (char item in chars)
        {
            if (item.Equals('。') || item.Equals('，'))
            {
                WordLine = Instantiate(ALine);
                WordLine.transform.SetParent(this.gameObject.transform);
                WordLine.transform.localScale = Vector3.one;
            }
            else
            {
                GameObject LittleWord = Instantiate(Word);
                LittleWord.transform.SetParent(WordLine.transform);
                LittleWord.GetComponent<Text>().text = item.ToString();
                LittleWord.transform.localScale = Vector3.one;
                WordsList.Add(LittleWord);
            }
        }
    }

    //延时
    private void TimeDelay(float tt, float t)
    {
        timer += Time.deltaTime;
        if (timer >= tt)
        {
            timer = 0;
            timerStart = true;
        }
        if (timer >= t && timerStart)
        {
            WordsList[index].GetComponent<Text>().DOFade(1, 1f);
            index++;
            timer = 0;
        }
    }
}
